package com.ehotting.edsta.circuitDesign.gen.element.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.domain.EsSceneParam;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ScriptTypeEnum;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AssignmentElementScript extends EqualElementScriptAbstract {

    /***
     * ----------赋值-------------
     * 找元器件，根据取值查找对应元器件，
     * 比如输入：
     * r1=200;r1.xx=300
     * r2=400;r2.yy=400
     * 找出对应值的元器件
     * @param esRuleScript
     * @param inputParamList
     * @param outputParamList
     * @return
     */
    public String invoke(EsRuleScript esRuleScript, List<EsCircuitCodeDataTemplate> inputParamList, List<EsSceneParam> outputParamList, String functionName){
        return  super.invoke(esRuleScript, inputParamList,  outputParamList,  functionName);
    }

    @Override
    public boolean isTrue(ScriptTypeEnum code) {
        return ScriptTypeEnum.ASSIGNMENT == code;
    }


    public String handleResult(List<EsSceneParam> outputParamList, int index){
        StringBuffer buf = new StringBuffer();
        buf.append( ScriptConstants.TAB + "if(componentList.size()>0){"+ ScriptConstants.LINE);
        for (int j=0;j<outputParamList.size();j++){
            EsSceneParam esSceneParam = outputParamList.get(j);
            buf.append(ScriptConstants.TAB(2) + "def map_"+index+"_"+j+" = ElementDataHelper.first(componentList,\""+esSceneParam.getCode()+"\");" + ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(2) + esSceneParam.getCode() + "List.clear();" + ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(2) + "if(map_"+index+"_"+j+"!=null){" + ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(3) + esSceneParam.getCode() + "List.add(" + "map_"+index+"_"+j+".get(\"" + esSceneParam.getCode() + "\")" + ");" + ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(3)+ "outMapList.add(map_"+index+"_"+j+");"+ ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(2) + "}" + ScriptConstants.LINE);
        }
        buf.append( ScriptConstants.TAB(2) + "componentList.clear();"+ ScriptConstants.LINE);
        buf.append( ScriptConstants.TAB + "}");
        buf.append(ScriptConstants.LINE);
        return buf.toString();
    }


    @Override
    public String getExample() {
        StringBuffer buf = new StringBuffer();
        buf.append("输入格式参数:");
        buf.append("输入格式:");
        buf.append("r1=200").append(ScriptConstants.LINE);
        buf.append("或者多行，回车隔开:");
        buf.append("r1=200").append(ScriptConstants.LINE);
        buf.append("r2=400").append(ScriptConstants.LINE);
        buf.append("或者多属性，分号隔开:");
        buf.append("r1=200;r1.xx=300").append(ScriptConstants.LINE);
        buf.append("r2=400;r2.yy=400").append(ScriptConstants.LINE);

        return buf.toString();
    }


}
